<?php
/**
 * Created by PhpStorm.
 * User: liu85
 * Date: 2019/2/24
 * Time: 14:11
 */

namespace app\admin\controller;


use app\admin\business\ProductBusiness;
use app\admin\validate\ProductAddValidate;
use app\common\helper\AdminInfoHelper;
use app\common\service\ProductService;
use think\App;

class Product extends Auth
{
    private $productBusiness;
    private $productModel;

    public function __construct(AdminInfoHelper $adminInfoHelper, App $app = null)
    {
        parent::__construct($adminInfoHelper, $app);
        $this->productBusiness = new ProductBusiness($adminInfoHelper);
        $this->productModel = new \app\common\model\Product();
    }


    public function getList()
    {
        $name = $this->request->get('name');
        $isHot = $this->request->get('isHot', -1);
        $isNew = $this->request->get('isNew', -1);
        $startTime = $this->request->get('startTime');
        $endTime = $this->request->get('endTime');
        list($list, $count) = $this->productBusiness->getList($name, $isHot, $isNew, $startTime, $endTime);
        $this->assign('page', $list->render());
        $list = ProductService::formatAdminProductList($list);
        $this->assign('list', $list);
        $this->assign('count', $count);
        $this->assign('name', $name);
        $this->assign('isHot', $isHot);
        $this->assign('startTime', $startTime);
        $this->assign('endTime', $endTime);
        $this->assign('isNew', $isNew);

        return $this->fetch('product_list');
    }

    /**
     * @return mixed
     */
    public function add()
    {
        return $this->fetch();
    }

    /**
     * 添加
     */
    public function doAdd()
    {
        $tags = $this->request->param('tags');
        $data = [
            'name' => $this->request->param('name'),
            'icon' => $this->request->param('icon'),
            'quota' => $this->request->param('quota'),
            'link' => $this->request->param('link'),
            'term' => $this->request->param('term'),
            'loanTime' => $this->request->param('loanTime'),
            'interestRate' => $this->request->param('interestRate'),
            'isNew' => isset($tags['isNew']) ? 1 : 0,
            'isHot' => isset($tags['isHot']) ? 1 : 0,
            'sort' => $this->request->param('sort'),
        ];
        $validate = new ProductAddValidate();
        if (!$validate->check($data)) {
            _failure($validate->getError());
        }
        $last = $this->productModel->save($data);
        self::doLast($last);
    }

    /**
     * 编辑
     */
    public function doEdit()
    {
        $id = $this->request->get('id');
        $info = $this->productModel->get($id);
        if(!$info){
            _failure('产品不存在或已删除,请刷新重试');
        }
        $tags = $this->request->param('tags');
        $data = [
            'name' => $this->request->param('name'),
            'icon' => $this->request->param('icon'),
            'quota' => $this->request->param('quota'),
            'link' => $this->request->param('link'),
            'term' => $this->request->param('term'),
            'loanTime' => $this->request->param('loanTime'),
            'interestRate' => $this->request->param('interestRate'),
            'isNew' => isset($tags['isNew']) ? 1 : 0,
            'isHot' => isset($tags['isHot']) ? 1 : 0,
            'sort' => $this->request->param('sort'),
        ];
        $validate = new ProductAddValidate();
        if (!$validate->check($data)) {
            _failure($validate->getError());
        }
        $last = $info->save($data);
        self::doLast($last);
    }

    /**
     * 编辑
     * @return mixed
     */
    public function edit()
    {
        $id = $this->request->get('id');
        $info = $this->productModel->get($id);
        if(!$info){
            _failure('产品不存在或已删除,请刷新重试');
        }
        $this->assign('info', $info);
        return $this->fetch();
    }

    /**
     * 删除
     * @throws \Exception
     */
    public function del()
    {
        $id = $this->request->param('id');
        $last = $this->productModel->delete($id);
        self::doLast($last);
    }
}